﻿using System;
using __Able.Network.Packets;
using __Able.Network.Utils;

namespace __ts.Core.Connection
{
    /// <summary>
    /// 连接策略
    /// </summary>
    public class ConnectionPolicy
    {
        /// <summary>
        /// 通道
        /// </summary>
        private Channel channel;
        /// <summary>
        /// 包辅助类
        /// </summary>
        private PacketHelper helper;

        public uint SupportedFamily { get; private set; }
        public uint RelocateFamily { get; private set; }

        /// <summary>
        /// 连接ID
        /// </summary>
        /// <value></value>
        public string ID { get; private set; }

        /// <summary>
        /// 连接ID
        /// </summary>
        /// <param name="channel"></param>
        /// <param name="id"></param>
        /// <param name="supportedFamily"></param>
        /// <param name="relocateFamily"></param>
        public ConnectionPolicy(Channel channel, string id, uint supportedFamily, uint relocateFamily)
        {
            this.ID = id;
            this.channel = channel;
            this.SupportedFamily = supportedFamily;
            this.RelocateFamily = relocateFamily;
            this.helper = new PacketHelper();
        }

        /// <summary>
        /// 一般网络错误时
        /// Called when [exception].
        /// </summary>
        /// <param name="e">The e.</param>
        public void OnNetworkError(Exception e)
        {
            channel.ConnectionManager.OnNetworkError(e);
        }
        /// <summary>
        /// 连接服务器错误时
        /// </summary>
        /// <param name="e"></param>
        public void OnConnectServerError(Exception e)
        {
            channel.ConnectionManager.OnConnectServerError(e);
        }
        /// <summary>
        /// 连接服务器成功后
        /// Called when [connected].
        /// </summary>
        public void OnConnected()
        {
            channel.ConnectionManager.OnConnectSuccessfully();
        }
        /// <summary>
        /// 创建一个错误包
        /// </summary>
        /// <param name="errorCode">The error code.</param>
        /// <returns></returns>
        public ErrorPacket CreateErrorPacket(ErrorPacketType errorCode, string portName, Exception e)
        {
            ErrorPacket errorPacket = new ErrorPacket(errorCode, channel.User, e);
            errorPacket.Family = SupportedFamily;
            errorPacket.ConnectionId = portName;
            return errorPacket;
        }
        /// <summary>
        /// 压入一个重发包
        /// </summary>
        /// <param name="outPacket">The out packet.</param>
        public void PushResend(_OutPacket outPacket, string portName)
        {
            channel.PacketManager.AddResendPacket(outPacket, portName);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inPacket"></param>
        public void PushIn(_InPacket inPacket)
        {
            channel.PacketManager.AddIncomingPacket(inPacket, ID);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="receiveIn"></param>
        public void PushIn(ByteBuffer receiveIn)
        {
            _InPacket inPacket = ParseIn(receiveIn);
            this.PushIn(inPacket);
        }
        /// <summary>
        /// 解析输入包
        /// </summary>
        /// <param name="buf">The buf.</param>
        /// <returns></returns>
        public _InPacket ParseIn(ByteBuffer buf)
        {
            return helper.ParseIn(SupportedFamily, buf, channel.User);
        }
        /// <summary>
        /// 记录发送历史
        /// </summary>
        /// <param name="outPacket"></param>
        public void PutSent(_OutPacket outPacket)
        {
            helper.PutSent(outPacket);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="packet"></param>
        /// <param name="add"></param>
        /// <returns></returns>
        public bool IsReplied(_OutPacket packet, bool add)
        {
            return helper.IsReplied(packet, add);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="inPacket"></param>
        /// <returns></returns>
        public _OutPacket RetrieveSent(_InPacket inPacket)
        {
            return helper.RetriveSent(inPacket);
        }
        /// <summary>
        /// 使用的代理信息
        /// </summary>
        /// <value></value>
        public Proxy Proxy
        {
            get { return channel.Proxy; }
        }
    }
}
